package com.boge.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boge.common.utils.ShiroUtils;
import com.boge.modules.shop.entity.GoodsEntity;
import com.boge.modules.shop.entity.GoodsnewEntity;
import com.boge.modules.shop.service.GoodsService;
import com.boge.modules.shop.service.GoodsnewService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.shop.dao.CollectDao;
import com.boge.modules.shop.entity.CollectEntity;
import com.boge.modules.shop.service.CollectService;


@Service("collectService")
public class CollectServiceImpl extends ServiceImpl<CollectDao, CollectEntity> implements CollectService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsnewService goodsnewService;

    @Autowired
    private SysUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity loginUser = ShiroUtils.getUserEntity();
        IPage<CollectEntity> page = this.page(
                new Query<CollectEntity>().getPage(params),
                new LambdaQueryWrapper<CollectEntity>()
                        .eq(!"admin".equals(loginUser.getUsername()),CollectEntity::getUserId,loginUser.getUserId())
        );

        List<CollectEntity> records = page.getRecords();
        if(records != null){
            for (CollectEntity record : records) {
                SysUserEntity userEntity = userService.getById(record.getUserId());
                if(userEntity != null){
                    record.setUserName(userEntity.getUsername());
                }
                Integer goodsId = record.getGoodsId();
                GoodsEntity goods = goodsService.getById(goodsId);
                if(goods != null){
                    record.setGoodsName(goods.getTitle());
                    record.setImgUrl(WebController.BASE_URL+goods.getImage());
                    record.setPrice(goods.getPrice());
                }else{
                    GoodsnewEntity goodsnewEntity = goodsnewService.getById(goodsId);
                    record.setGoodsName(goodsnewEntity.getComment());
                    record.setImgUrl(WebController.BASE_URL+goodsnewEntity.getImg());
                    record.setPrice(goodsnewEntity.getPrice());
                }
            }
        }

        return new PageUtils(page);
    }

}